﻿using System;
using System.Net.Http;
using System.Web.Http.Tracing;

namespace WebApiSelfHost.Filters
{
    public class TraceWriter : ITraceWriter
    {
        public void Trace(HttpRequestMessage request, string category, TraceLevel level, Action<TraceRecord> traceAction)
        {
            TraceRecord rec = new TraceRecord(request, category, level);
            traceAction(rec);
            WriteTrace(rec);
        }

        protected void WriteTrace(TraceRecord rec)
        {

            var args = new object[]
            { 
                rec.RequestId,
                Environment.NewLine,
                rec.Kind,
                Environment.NewLine,
                rec.Status,
                Environment.NewLine,
                rec.Operation,
                Environment.NewLine,
                rec.Operator,
                Environment.NewLine,
                rec.Category,
                Environment.NewLine,
                rec.Request,
                Environment.NewLine,
                rec.Message,
                Environment.NewLine,
                "-----------------------------------------------------------------------"
            };
            
            string message = string.Format("RequestId: {0};{1}Kind: {2};{3}Status: {4};{5}Operation: {6};{7}Operator: {8};{9}Category: {10};{11}Request: {12};{13}Message: {14};{15}{16}", args);
            //System.Diagnostics.Trace.WriteLine(message, rec.Category);
            Console.WriteLine(message);
        }
    }
}
